
#include "FPS.hpp"

float gElapsedTimeTotal = 0.0f;
float gFrameCount = 0.0f;

FPS::FPS()
{
     ZeroMemory(&mCounterStart, sizeof(mCounterStart));
     mFreq = 0.0f;

     mFPSAvg = 0.0f;
}

void FPS::StartCounter()
{
     LARGE_INTEGER li;
     QueryPerformanceFrequency(&li);
     mFreq = float(li.QuadPart) / CONVERT_TO_MILISECONDS;

     QueryPerformanceCounter(&li);
     mCounterStart = li;
}

//Returns the elapsed time in miliseconds for every frame as long as this function is called every frame
float FPS::PumpFrame()
{
     static const float UPDATE_MODIFIER = 0.5f;
     LARGE_INTEGER li;
     QueryPerformanceCounter(&li);
     float elapsedTime = float( li.QuadPart - mCounterStart.QuadPart) / mFreq;

     mCounterStart = li;

     gElapsedTimeTotal += elapsedTime;

     if( gElapsedTimeTotal > (CONVERT_TO_MILISECONDS * UPDATE_MODIFIER) ) //update every 500 miliseconds
     {
          mFPSAvg = gFrameCount / UPDATE_MODIFIER;
          gFrameCount = 0;
          gElapsedTimeTotal = 0.0f;
     }

     gFrameCount += 1.0f;

     return elapsedTime;
}

float FPS::GetFPSAvg() const 
{ 
     return mFPSAvg; 
}